package com.jinyi.wwdeng.ui.steps.adapter

import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jinyi.lib_baseadapter.BGAOnItemChildClickListener
import com.jinyi.lib_baseadapter.BGARecyclerViewAdapter
import com.jinyi.lib_baseadapter.BGAViewHolderHelper
import com.jinyi.wwdeng.App
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.utils.event.TrackEventType
import com.jinyi.wwdeng.utils.event.TrackEventUtils
import com.jinyi.wwdeng.utils.popupwindow.ListCenterPopupWindow
import com.jinyi.wwdeng.utils.recyclerview.LinearSpacingItemDecoration
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/18 10:43
 * @Annotation：流程 - 联系人适配器
 */
class StepContactInfoAdapter(recyclerView: RecyclerView?) : BGARecyclerViewAdapter<JSONObject>(recyclerView, R.layout.item_step_contact_info_brand) {

    var parentView: ViewGroup? = null
    override fun fillData(helper: BGAViewHolderHelper, position: Int, model: JSONObject) {
        helper.setText(R.id.tvBrandTitle,model.optString("brandTitle"))

        val recyclerView = helper.getView<RecyclerView>(R.id.recyclerView)
        recyclerView.setHasFixedSize(true)
        recyclerView.isNestedScrollingEnabled = false
        recyclerView.isFocusableInTouchMode = false //不需要焦点
        recyclerView.layoutManager = LinearLayoutManager(mContext)
        if (recyclerView.itemDecorationCount == 0){
            val topDecoration = App.dp2px(14f)
            recyclerView.addItemDecoration(LinearSpacingItemDecoration(0,0,0,topDecoration))
        }
        val commonAdapter = StepCommonAdapter(recyclerView)
        commonAdapter.typeFaceStyle = Typeface.NORMAL
        recyclerView.adapter = commonAdapter
        recyclerView.itemAnimator = DefaultItemAnimator()
        commonAdapter.setOnItemChildClickListener(object : BGAOnItemChildClickListener {
            override fun onItemChildClick(parent: ViewGroup?, childView: View?, childPosition: Int) {
                val childModel = commonAdapter.getItem(childPosition)
                val dataList = arrayListOf<JSONObject>()
                val dataArray = childModel.optJSONArray("values")
                for (i in 0..<dataArray.length()){
                    dataList.add(dataArray.optJSONObject(i))
                }
                showSelectPopupwindow(childView!!,dataList) { data ->
                    childModel.put("selectItem", data)
                    commonAdapter.notifyItemChanged(childPosition)
                    TrackEventUtils.logEvent(String.format(TrackEventType.CLICK.CLICK_STEP_XX_SELECT,childModel.optString("paramName")),data.optString("value"))
                }
            }
        })

        val dataArray = model.optJSONArray("dataList")
        val dataList = arrayListOf<JSONObject>()
        for (i in 0..<dataArray.length()){
            dataList.add(dataArray.optJSONObject(i))
        }
        commonAdapter.data = dataList
    }

    fun interface IChildItemSelectListener{
        fun onChildSelectClick(data: JSONObject)
    }

    /**
     * 选择选项
     */
    private fun showSelectPopupwindow(view: View,dataList: ArrayList<JSONObject>,listener: IChildItemSelectListener){
        val selectPopup = ListCenterPopupWindow(mContext)
        selectPopup.setWindowWidth(view.width + App.dp2px(10f))
        selectPopup.setSelectItemListener(object : ListCenterPopupWindow.IItemClickListener {
            //完成
            override fun onConfirmAction(data: JSONObject?) {
                listener.onChildSelectClick(data!!)
            }
            //关闭监听
            override fun onDismissListener() {
            }
        })
        selectPopup.show(view, parentView!!)
        selectPopup.setBrandList(dataList)
    }
}